package com.itheima.reggie.commons;

import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.exception.MaxUploadSizeExceededException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.SQLInvalidAuthorizationSpecException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandle {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public R<String> handleCustomException(MaxUploadSizeExceededException ex) {
        return R.error("文件最大不能超过20Mb");
    }
    @ExceptionHandler(CustomException.class)
    public R<String> handleCustomException(CustomException ex) {
        return R.error(ex.getMessage());
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException ex) {
        String message = ex.getMessage();
        return R.error("添加失败，该商品已经存在");
    }
}
